package com.hdjd.service.imp;

import com.hdjd.dao.AccountDao;
import com.hdjd.dao.FundRecordDao;
import com.hdjd.dao.GoodsDao;
import com.hdjd.dao.OrderDetailDao;
import com.hdjd.entities.Account;
import com.hdjd.entities.FundRecord;
import com.hdjd.entities.OrderDetail;
import com.hdjd.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @author 刘子强
 * 2021/1/23 17:21
 */
@Service
public class BusinessServiceImp implements BusinessService {
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private OrderDetailDao orderDetailDao;
    @Autowired
    private FundRecordDao fundRecordDao;
    @Autowired
    private GoodsDao goodsDao;



    //商家注册
    @Override
    public boolean bussinessRegister(Integer accId, String accPwd, String accTelephone, String shopName) {
        boolean flag = false;
        Account account = this.accountDao.selectAccountByAccIdAndAccPwd(accId, accPwd);
        if(account != null){
            account.setAccTelephone(accTelephone);
            account.setShopName(shopName);
            account.setBusinessSign(1);
            flag = this.accountDao.updateAccountByAccId(account);
        }
        return flag;
    }

    //查询商家所有未完成订单
    @Override
    public List<OrderDetail> selectUnFinishOrder(Integer accId, Integer ordStatus, Integer pageCurrent, Integer pageSize) {
        List<OrderDetail> orderDetails = this.orderDetailDao.selectUnFinishOrder(accId, ordStatus, (pageCurrent-1)*pageSize, pageSize);
        return orderDetails;
    }


    //商家发货
    @Override
    public boolean deliverGoods(Integer detId) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setDetId(detId);
        orderDetail.setOrdStatus("3");
        boolean flag = this.orderDetailDao.updateOrderDetailByDetId(orderDetail);
        return flag;
    }

    //商家退款
    @Override
    @Transactional  //事务管理
    public boolean refund(Integer detId, Integer accId, Double totalPrice) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setDetId(detId);
        orderDetail.setOrdStatus("9");
        //1.改变订单状态
        this.orderDetailDao.updateOrderDetailByDetId(orderDetail);
        //2.改变买家余额
        this.accountDao.updateAccBalanceByAccId(accId, totalPrice);
        //3.查询商家余额
        Double accBalance = this.accountDao.selectAccBalanceByAccId(accId);

        FundRecord fundRecord = new FundRecord();
        fundRecord.setAccId(accId);
        fundRecord.setDealAmount(totalPrice);
        fundRecord.setRecordBalance(accBalance);
        fundRecord.setRecordType(6);
        fundRecord.setDetId(detId);
        //获取系统当前时间，即记录添加时间
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String addTime = df.format(date);
        fundRecord.setAddTime(addTime);
        //4.向资金记录表添加退款记录
        boolean flag = this.fundRecordDao.insertFundRecord(fundRecord);

        return flag;
    }

    //查询商家所有已完成的订单
    @Override
    public List<OrderDetail> selectFinishOrder(Integer accId, Integer ordStatus, Integer pageCurrent, Integer pageSize, String startDate) {
        //获取系统当前日期
        Date date = new Date();
        Calendar c = new GregorianCalendar();
        //将当前日期变为日历，并加 1 天，再转换回来变成日期，因为sql语句 between date1 and date2 ，不包含date2
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, 1);
        date = c.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = df.format(date);

        List<OrderDetail> orderDetails = this.orderDetailDao.selectFinishOrder(accId, ordStatus, (pageCurrent-1)*pageSize, pageSize, startDate, currentDate);
        return orderDetails;
    }

    //查询商家所有已完成订单数据笔数
    @Override
    public Integer countFinishOrder(Integer accId, Integer ordStatus, String startDate) {
        //获取系统当前日期
        Date date = new Date();
        Calendar c = new GregorianCalendar();
        //将当前日期变为日历，并加 1 天，再转换回来变成日期，因为sql语句 between date1 and date2 ，不包含date2
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, 1);
        date = c.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = df.format(date);

        Integer count = this.orderDetailDao.countFinishOrder(accId,ordStatus,startDate,currentDate);
        return count;
    }

    @Override
    public Integer countUnFinishOrder(Integer accId, Integer ordStatus) {
        Integer count = this.orderDetailDao.countUnFinishOrder(accId,ordStatus);
        return count;
    }

}
